package com.atde._03_creational_design_patterns.prototype.improve;

/**
 * @BelongsProject: DesignPattern
 * @BelongsPackage: com.atde._03_creational_design_patterns.prototype
 * @Author: Del
 * @CreateTime: 2023-08-09  23:07
 * @Description: 原型模式 -- 克隆羊  -- 传统实现
 * @Version: 1.0
 */
public class Sheep implements Cloneable{
    private String name;
    private int age;
    private String color;
    // 如果是对象，在克隆时会如何处理？
    public Sheep friend;

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }


    // 克隆该实例，使用默认的clone方法来完成
    @Override
    protected Object clone(){
        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (Exception e) {
            System.out.println("克隆失败: " + e.getMessage());
        }
        return sheep;
    }
}
